define(['angular',"text!www/trends/picture_info.html",'url','common'], function (angular,tpl) {
    //angular会自动根据controller函数的参数名，导入相应的服务
   function controller($timeout,$scope,$rootScope,$http,$routeParams,$anchorScroll,$location){
	   $anchorScroll();
	   showSpin();
		rightMenu();
		$scope.comment = "";
		var picture = function (newsId) {
			this.news_info={};
			this.imgs = [];
			this.likes = [];
			this.comments = [];
			this.baseUrl = ip;
			this.adUrl = ip+"/"+"data/afficheimg/";
			this.busy = false;
			this.info = {};
			this.news_show = false;
			this.news_id = newsId;
			this.placeholder = "我也来吐槽";
			this.parent_id = 0;
			this.new_index = -1;
	    };
		$scope.picture =new picture($routeParams.newsId);
		
		
		$scope.picture.getNewsInfo = function(){
		   	 if (this.busy){
		       	 hideSpin();
		       	 return;
		        }
		    this.busy = true;
		    var pro = this;
	   	 	var url = cl.getNewsInfoUrl+"&news_id="+pro.news_id+"&callback=JSON_CALLBACK" ;
	        $http.jsonp(url).success(function (res) {
					if(res.code=="CL02031"){//获取成功
						pro.news_info = res.result.news;
						pro.imgs = res.result.img;
						pro.likes = res.result.likes;
						pro.comments = res.result.comment;
						pro.info = res.result.info;
						pro.news_show = true;
					}else{
						showAlert("获取不到该帖信息，可能已被删除");
						$rootScope.changeNull();
					}
					hideSpin();
					pro.busy = false;
	        });
	    };
	    $scope.picture.delLike = function(){
//	    	var array = [];
//	    	for(var k in this.likes){
//	    		if(this.likes[k].liker != this.info.wxid){
//	    			array.push(this.likes[k]);
//	    		}
//	    	}
//	    	this.likes = array;
	    	for(var k in this.likes){
	    		if(this.likes[k].liker == this.info.wxid){
	    			this.likes.splice(k,1);
	    		}
	    	}
	    };
	    $scope.picture.likeNew = function(){
	    	if(!this.info.wxid){
	    		showAlert("登录后方可点赞,3秒后将自动登录");
	    		setTimeout("window.location.href=cl.wxLoginUrl;",3000);
	    		
	    		return false;
	    	}
	    	if (this.busy){
		       	 hideSpin();
		       	 return;
		        }
		    this.busy = true;
		    var pro = this;
		    var url = cl.likeNewUrl+"&wxid="+this.info.wxid +"&news_id="+pro.news_id+"&callback=JSON_CALLBACK" ;
		    $http.jsonp(url).success(function (res) {
				if(res.code=="CL02036"){//获取成功
					if(res.result.codeMsg=="cancel"){
						pro.news_info.like_num =  parseInt(pro.news_info.like_num) - 1;
						pro.delLike();
						pro.news_info.is_like = "";
						showAlert("取消点赞");
					}else{
						pro.news_info.like_num = parseInt(pro.news_info.like_num) + 1;
						var li = {};
						li.liker = pro.info.wxid;
						li.headimgurl = pro.info.headimgurl;
						pro.likes.push(eval(li));
						pro.news_info.is_like = "liked";
						showAlert("成功点赞");
					}
				}else{
					showAlert("点赞失败，请登录后再操作");
				}
				pro.busy = false;
		    });
	    };
		$scope.picture.getNewsInfo();
		
		
		$scope.picture.reply = function(name,parent_id,new_index){
			if(!$("#comment").val()){
				this.placeholder = "回复 "+name+":";
				this.parent_id = parent_id;
				this.new_index = new_index;
				document.getElementById('comment').focus();
			}
		};
		$scope.picture.restore = function(){
			if(!$("#comment").val()){
				this.placeholder = "我也来吐槽";
				this.parent_id = 0;
				this.new_index = -1;
				document.getElementById('comment').focus();
			}
		};
		var  chatMaxNum = 15;
		$scope.picture.replyClick = function(){
			if(this.info.wxid==0||this.info.wxid==""||this.info.wxid==null||this.info.wxid==undefined){
				showAlert("登录后方可吐糟,3秒后将自动登录");
				setTimeout("window.location.href=cl.wxLoginUrl;",3000);
	    		
				return false;
			}
			var comment = $("#comment").val();
			if(comment==""||comment==null||comment==undefined){
				showAlert("吐槽几句吧~");
				return false;
			}
			var length = getBLen(comment);
			if(length>chatMaxNum){
				showMsg("吐槽点太多啦，少点字吧~");
				isSend = false;
				return false;
			}
			
			var params = "&comment="+comment+"&wxid="+this.info.wxid+"&news_id="+this.news_info.id
						+"&parent_id="+this.parent_id;
			var url = cl.replyUrl+"&callback=JSON_CALLBACK"+params ;
			var pro = this;
			 $http.jsonp(url).success(function (res) {
					if(res.code=="CL02040"){//获取成功
						var com = {};
						com.id = res.result.id;
						com.comment_time = res.result.comment_time;
						com.comment = comment;
						com.commenter = pro.info.wxid;
						com.headimgurl = pro.info.headimgurl;
						com.name = pro.info.name;
						if(pro.parent_id!=0){//二级评论
							com.parentComment = {};
							com.parentComment.name = pro.comments[pro.new_index].name;
							com.parentComment.comment = pro.comments[pro.new_index].comment;
							pro.comments.unshift(eval(com));
						}else{//一级评论
							pro.comments.unshift(eval(com));
						}
						pro.goToAnchor();
						$("#comment").val('');
						pro.placeholder = "我也来吐槽";
						pro.parent_id = 0;
						pro.new_index = -1;
						pro.news_info.comment_num = parseInt(pro.news_info.comment_num)+1;
						showAlert("吐槽成功~");
					}else if(res.code=="CL02039"){
						showAlert("吐槽几句吧~");
					}else{
						showAlert("评论失败，请登录后再操作");
					}
					pro.busy = false;
			    });
		};
		$scope.picture.goToAnchor = function () {
			var old = $location.hash();
	          $location.hash("comment-top");
	          $anchorScroll();
	          $location.hash(old)
	    };
		$scope.picture.delComment = function(id,index){
			if (this.busy)
			{
		       	 return;
	        }
		    this.busy = true;
		    showSpin();
			var url = cl.delCommentUrl+"&callback=JSON_CALLBACK&id="+id+"&wxid="+this.info.wxid+"&news_id="+this.news_id;
			var pro = this;
			showConfirm("确定删除？",function()
			{
				 $http.jsonp(url).success(function (res) {
						if(res.code=="CL02043"){//删除成功
							showAlert("成功删除评论");
							pro.news_info.comment_num = parseInt(pro.news_info.comment_num)-1;
			    			pro.comments.splice(index,1);
						}else{
							showAlert("删除评论失败");
						}
						pro.busy = false;
						hideSpin();
				 })
			},function(){
				pro.busy = false;
				hideSpin();
			});
		};
		$scope.picture.seeImg = function(index){
			console.info(222);
			var pro = this;
	 		var items = pro.imgs;
	 		var urls = [];
            for (var i = 0; i < items.length; i++) {
           	  urls[i] =pro.baseUrl+items[i].img;
            }
	 		wx.previewImage({
	 		    current: pro.baseUrl+pro.imgs[index].img, // 当前显示图片的http链接
	 		    urls: urls // 需要预览的图片http链接列表
	 		});
		};
		$scope.picture.collectNews = function(){
			 if (this.busy){
	        	 return;
	         }
	    	 if(!this.info.wxid>0)
	         {
	    		 showAlert("登录后方可收藏,3秒后将自动登录");
	    		 setTimeout("window.location.href=cl.wxLoginUrl;",3000);
		    		
	    		 return;
	         }
	    	 this.busy = true;
	    	 var pro = this;
	         var url = cl.collectNewUrl+"&wxid="+this.info.wxid+"&news_id="+this.news_info.id+"&callback=JSON_CALLBACK" ;
	         $http.jsonp(url).success(function (res) {
					if(res.code=="CL02026"){//获取成功
						if(res.result.codeMsg=="cancel"){
							showAlert("取消收藏");
							pro.news_info.collect_times =  parseInt(pro.news_info.collect_times) - 1;
							pro.news_info.is_collect = " ";
						}else{
							showAlert("已加入收藏");
							pro.news_info.collect_times =  parseInt(pro.news_info.collect_times) + 1;
							pro.news_info.is_collect = "collected";
						}
					}else{
						showAlert("收藏失败，请登录后再操作");
					}
					pro.busy = false;
	         });
		};
    };
    return {controller:controller,tpl:tpl};
});
function getBLen(str) {
	  if (str == null) return 0;
	  if (typeof str != "string"){
	    str += "";
	  }
	  return str.replace(/[^\x00-\xff]/g,"00").length;
}
